package com.earnings.okhttputils.utils.god.GodAdapter;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.AbsListView;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

/**
 * Created by Administrator on 2017/5/20 0020.
 */

public class GodViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> views;
    public View itemView;

    public GodViewHolder(View itemView) {
        super(itemView);
        AutoUtils.auto(itemView);
        this.itemView = itemView;
        this.views = new SparseArray<>();
    }

    public GodViewHolder setText(int viewId, String text) {
        TextView textView = getView(viewId);
        textView.setText(text);
        return this;
    }

    public GodViewHolder setTextColor(int viewId, int colorId) {
        TextView textView = getView(viewId);
        textView.setTextColor(colorId);
        return this;
    }


    public GodViewHolder setRating(int viewId, float rating, int max) {
        RatingBar view = getView(viewId);
        view.setMax(max);
        view.setRating(rating);
        return this;
    }

    public GodViewHolder setRating(int viewId, float rating) {
        RatingBar view = getView(viewId);
        view.setRating(rating);
        return this;
    }

    public GodViewHolder setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    public GodViewHolder setImageDrawable(int viewId, int imgid) {
        ImageView view = getView(viewId);
        view.setImageResource(imgid);
        return this;
    }

    public GodViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }


    public GodViewHolder setBackgroundResource(int viewId, int backgroundResource) {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundResource);
        return this;
    }
    public GodViewHolder setBackgroundColor(int viewId, int backgroundResource) {
        View view = getView(viewId);
        view.setBackgroundColor(backgroundResource);
        return this;
    }


    public GodViewHolder setAlphaAnimation(int viewId, float fromAlpha, float toAlpha, long duration) {
        View view = getView(viewId);
        Animation animation = new AlphaAnimation(fromAlpha, toAlpha);
        animation.setDuration(duration);
        view.startAnimation(animation);
        return this;
    }


    public GodViewHolder setImageResource(int viewId, int imageResId) {
        ImageView view = getView(viewId);
        view.setImageResource(imageResId);
        return this;
    }

    public GodViewHolder setTag(int viewId, Object tag) {
        View view = getView(viewId);
        view.setTag(tag);
        return this;
    }

    public GodViewHolder setTag(int viewId, int key, Object tag) {
        View view = getView(viewId);
        view.setTag(key, tag);
        return this;
    }

    public GodViewHolder setChecked(int viewId, boolean checked) {
        View view = getView(viewId);
        if (view instanceof CompoundButton) {
            ((CompoundButton) view).setChecked(checked);
        } else if (view instanceof CheckedTextView) {
            ((CheckedTextView) view).setChecked(checked);
        }
        return this;
    }


    public GodViewHolder setListViewAdapter(int viewId, ListAdapter adapter) {
        View view = getView(viewId);
        if (view instanceof AbsListView) {
            ((ListView) view).setAdapter(adapter);
        }
        return this;
    }


    public GodViewHolder setRecyclerViewAdapter(int viewId, RecyclerView.Adapter adapter) {
        View view = getView(viewId);
        if (view instanceof RecyclerView) {
            ((RecyclerView) view).setAdapter(adapter);
        }
        return this;
    }

    public GodViewHolder setRecyclerViewLayoutManager(int viewId, RecyclerView.LayoutManager layoutManager) {
        View view = getView(viewId);
        if (view instanceof RecyclerView) {
            ((RecyclerView) view).setLayoutManager(layoutManager);
        }
        return this;
    }

    public GodViewHolder scrollTo(int viewId, int x, int y) {
        View view = getView(viewId);
        view.scrollTo(x, y);
        return this;
    }

    public GodViewHolder setVisible(int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public GodViewHolder setVisibility(int viewId, int visible) {
        View view = getView(viewId);
        switch (visible) {
            case View.VISIBLE:
                view.setVisibility(View.VISIBLE);
                break;
            case View.INVISIBLE:
                view.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                view.setVisibility(View.GONE);
                break;
            default:
                break;
        }

        return this;
    }


    public <T extends View> T getView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }


}
